<template>
  <div class="orders">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 搜索 -->
      <el-row>
        <el-input
          style="width: 300px"
          v-model="paginationInfo.query"
          @change="searchValue"
        ></el-input
        ><el-button
          class="el-icon-search"
          style="height: 40px"
          @click="searchValue"
        ></el-button>
      </el-row>

      <!-- 表格 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="#" width="50"> </el-table-column>
        <el-table-column prop="order_number" label="订单编号" width="180">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格" width="180">
        </el-table-column>
        <el-table-column prop="address" label="是否付款">
          <template v-slot="scope">
            <el-tag
              effect="dark"
              v-if="scope.row.order_pay == '0'"
              type="danger"
              size="mini"
              >未付款</el-tag
            >
            <el-tag effect="dark" size="mini" v-else type="success"
              >已付款</el-tag
            >
          </template>
        </el-table-column>

        <el-table-column prop="is_send" label="是否发货"> </el-table-column>

        <el-table-column prop="address" label="下单时间">
          <template v-slot="scope">
            {{ scope.row.create_time | filterTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-tooltip effect="dark" content="修改订单地址" placement="top">
              <el-button
                type="primary"
                @click="editLocation(scope.row)"
                class="el-icon-edit"
              ></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="物流信息" placement="top">
              <el-button
                type="success"
                @click="viewLocationDetail(scope.row)"
                class="el-icon-location"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="paginationInfo.pagenum"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="paginationInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 操作  修改地址 -->
    <el-dialog
      title="修改地址"
      :visible.sync="editDialog"
      width="60%"
      :before-close="handleClose"
    >
      <el-form
        :model="addressForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="city">
          <el-cascader
            v-model="addressForm.city"
            :options="options"
            :props="props"
            @change="handleChange"
            style="width: 100%"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="location">
          <el-input
            v-model="addressForm.location"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelEdit">取 消</el-button>
        <el-button type="primary" @click="submitEdit">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 查看物流信息 -->
    <el-dialog
      title="物流进度"
      :visible.sync="viewDialog"
      width="50%"
      :before-close="handleClose"
    >
      <el-timeline :reverse="false">
        <el-timeline-item
          v-for="(step, index) in stepsData"
          :key="index"
          :color="index == 0 ? '#0f0' : '#ccc'"
          :timestamp="step.time"
        >
          {{ step.context }}
        </el-timeline-item>
      </el-timeline>

      <span slot="footer" class="dialog-footer">
        <el-button @click="viewDialog = false">取 消</el-button>
        <el-button type="primary" @click="viewDialog = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import cityOptions from '../../../provinces-china/city_data2017_element'
export default {
  data() {
    return {
      tableData: [],
      paginationInfo: {
        query: '',
        pagesize: 5, //每页数量
        pagenum: 1, //当前页
      },
      total: 10,
      pagesize: 5,
      current: 1,
      inputValue: '',
      editDialog: false,
      viewDialog: false,

      addressForm: {
        location: '',
        city: '',
      },
      rules: {
        city: [{ required: true, message: '请选择省市区县', trigger: 'blur' }],
        location: [{ required: true, message: '请填写地址', trigger: 'blur' }],
      },
      options: cityOptions,
      props: {
        expandTrigger: 'click',
        label: 'label',
        value: 'value',
        children: 'children',
      },

      stepsData: [],
    }
  },
  filters: {
    filterTime(val) {
      return new Date(val).toLocaleString()
    },
  },
  created() {
    this.getTableData()
  },
  methods: {
    async getTableData() {
      const { meta, data } = await this.$axios.get('/api/orders', {
        params: { ...this.paginationInfo },
      })
      if (meta.status == 200) {
        this.tableData = data.goods
        this.total = data.total
        // console.log(data, this.paginationInfo)
      } else {
        this.$message.error('获取数据失败')
      }
    },
    handleSizeChange(num) {
      //改变每页数量
      this.paginationInfo.pagesize = num
      this.getTableData()
    },
    handleCurrentChange(current) {
      //改变当前页为。。。
      this.paginationInfo.pagenum = current
      this.getTableData()
    },

    searchValue() {
      //搜索
      this.getTableData()
    },
    editLocation() {
      this.editDialog = true
    },
    async viewLocationDetail(row) {
      const { data, meta } = await this.$axios.get(`/api/kuaidi/1106975712662`)
      if (meta.status == 200) {
        this.viewDialog = true

        this.stepsData = data
        console.log(data, row)
      }
    },
    handleChange(val) {
      //级联

      console.log(val)
    },
    submitEdit() {
      this.$refs.ruleForm.validate((valid) => {
        console.log(valid)
        if (valid) {
          this.editDialog = false
          this.$message.success('提交成功')
          this.$nextTick(() => {
            this.$message.info('后台未配置接口，无需等待回应')
          })
        }
      })
    },
    cancelEdit() {
      this.$refs.ruleForm.resetFields()
      this.editDialog = false
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(() => {
          done()
          this.stepsData = []
        })
        .catch(() => {})
    },
  },
}
</script>
